package jia.game;

import org.apache.log4j.Logger;

import java.awt.*;
import java.io.FileInputStream;
import java.io.IOException;

/**
 *
 */
public class ComponentUtils {
    
    private static Font dungeonFont = null;
    private static final Logger log = Logger.getLogger(ComponentUtils.class);
    
    public static Color getGlobalLineColor(){
        return new Color(52, 47, 38);
        //return Color.RED;
    }
    
    public static Color getGlobalBackgroundColor(){
        return Color.BLACK;
    }
    
    public static Color getGlobalForegroundColor(){
        return Color.WHITE;
    }
    
    public static Font getGlobalFont(){
        
        try {
            FileInputStream in = new FileInputStream(loadExternalFontPath());
            dungeonFont = Font.createFont(Font.TRUETYPE_FONT, in);
            in.close();
            dungeonFont = dungeonFont.deriveFont(Font.PLAIN, 16);
            
        } catch (FontFormatException e) {
            if (GenericFlags.errorMessages){
                log.error("Could not load the font with this format!" + e.getMessage());
            }
        } catch (IOException e) {
            if (GenericFlags.errorMessages){
                log.error("Could not load the font file!" + e.getMessage());
            }
        }
        
        return dungeonFont;
        
    }
    
    public static Font getAlternateGlobalFont(){
        
        try {
            FileInputStream in = new FileInputStream(loadExternalAltFontPath());
            dungeonFont = Font.createFont(Font.TRUETYPE_FONT, in);
            in.close();
            dungeonFont = dungeonFont.deriveFont(Font.PLAIN, 12);
        } catch (FontFormatException e) {
            if (GenericFlags.errorMessages){
                log.error("Could not load the font with this format!" + e.getMessage());
            }
        } catch (IOException e) {
            if (GenericFlags.errorMessages){
                log.error("Could not load the font file!" + e.getMessage());
            }
        }
        
        return dungeonFont;
        
    }
    
    
    
    /**
     * Method to get the default font using an argument to determine its size.
     * @param fontSize
     * @return
     */
    public static Font getGlobalFont(int fontSize){
        
        try {
            FileInputStream in = new FileInputStream(loadExternalFontPath());
            dungeonFont = Font.createFont(Font.TRUETYPE_FONT, in);
            in.close();
            dungeonFont = dungeonFont.deriveFont(Font.PLAIN, fontSize);
        } catch (FontFormatException e) {
            if (GenericFlags.errorMessages){
                log.error("Could not load the font with this format!" + e.getMessage());
            }
        } catch (IOException e) {
            if (GenericFlags.errorMessages){
                log.error("Could not load the font file!" + e.getMessage());
            }
        }
        
        return dungeonFont;
        
    }
    public static Font getAlternateGlobalFont(int fontSize){
        
        try {
            FileInputStream in = new FileInputStream(loadExternalAltFontPath());
            dungeonFont = Font.createFont(Font.TRUETYPE_FONT, in);
            in.close();
            dungeonFont = dungeonFont.deriveFont(Font.PLAIN, fontSize);
        } catch (FontFormatException e) {
            if (GenericFlags.errorMessages){
                log.error("Could not load the font with this format!" + e.getMessage());
            }
        } catch (IOException e) {
            if (GenericFlags.errorMessages){
                log.error("Could not load the font file!" + e.getMessage());
            }
        }
        
        return dungeonFont;
        
    }
    
    private static String loadExternalFontPath(){
        return "resources/fonts/arial.ttf";
    }
    private static String loadExternalAltFontPath(){
        return "resources/fonts/arial.ttf";
    }
}
